extends Camera2D

var speed_scale = 0.1
var speed_move = 5
var zoom_min = 0.4
var zoom_max = 2.0

func _ready():
	zoom = Vector2(zoom_min,zoom_min)

func _process(_delta):
	var scale_xy:float = 0
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_AMPLIFY) == 1:
		scale_xy = speed_scale
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_REDUCE) == 1:
		scale_xy = -speed_scale
	if zoom.x+scale_xy>zoom_max || zoom.x+scale_xy<zoom_min:
		scale_xy = 0
	zoom += Vector2(scale_xy,scale_xy)
	
	var move_x:float = 0.0
	var move_y:float = 0.0
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_LEFT) == 1:
		move_x = -speed_move
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_RIGHT) == 1:
		move_x = speed_move
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_UP) == 1:
		move_y = -speed_move
	if Input.get_action_strength(KeyBoardInputManager.KEYCODE_DOWM) == 1:
		move_y = speed_move
	if move_x!=0 && move_y!=0:
		move_x /= 1.414
		move_y /= 1.414
	offset += Vector2(move_x,move_y)
	if KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_CENTER):
		offset = Vector2(0,0)
		zoom = zoom_min



var last_position = Vector2(0,0)
func _unhandled_input(event):
	if event is InputEventMouseMotion:
		if event.pressure == 1:
			offset += (last_position - event.position)*(Vector2(1,1)/zoom)
			last_position = event.position
	if event is InputEventMouseButton:
		var scale_xy:float = 0
		if event.button_index == MouseButton.MOUSE_BUTTON_WHEEL_UP:
			scale_xy = speed_scale
		if event.button_index == MouseButton.MOUSE_BUTTON_WHEEL_DOWN:
			scale_xy = -speed_scale
		if zoom.x+scale_xy>zoom_max || zoom.x+scale_xy<zoom_min:
			scale_xy = 0
		zoom += Vector2(scale_xy,scale_xy)
		if event.button_index == MouseButton.MOUSE_BUTTON_LEFT:
			last_position = event.position
